//
//  AutoScrollLabel.m
//  LotteryClient
//
//  Created by Sunyon 14-5-21.
//
//

#import "AutoScrollLabel.h"
@interface AutoScrollLabel()
{
	BOOL _started;

	UILabel *_label1;
	UILabel *_label2;

	UILabel *_currentLabel;

	CGPoint _orginPoint;

	NSArray *_titileArrayx;

	NSTimer *_timer;
}
@end
@implementation AutoScrollLabel
@dynamic titleArray;
- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self)
	{
        // Initialization code
		_interval = 5;
		_currentIndex = 0;
	    _label1 = [[UILabel alloc] initWithFrame: self.bounds];
		_label2 = [[UILabel alloc] initWithFrame: self.bounds];

		_orginPoint = CGPointMake(self.bounds.size.width, 0);
		_label1.backgroundColor = [UIColor clearColor];
		_label2.backgroundColor = [UIColor clearColor];

		_currentLabel = _label1;

		_label1.textAlignment = NSTextAlignmentCenter;
		_label2.textAlignment = NSTextAlignmentCenter;

		_timer = nil;
		self.clipsToBounds = YES;

		[self addSubview:_label2];
		[self addSubview:_label1];

		UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
																					 action:@selector(handleTapEvent)];
		[self addGestureRecognizer:tapGesture];

        [self setUserInteractionEnabled:YES];

    }
    return self;
}

- (void)dealloc
{
	if (_timer)
	{
		[_timer invalidate];
		_timer = nil;
	}
}

- (void)layoutSubviews
{
	[super layoutSubviews];

	_label1.frame =  self.bounds;
	_label2.frame =  self.bounds;

	_orginPoint = CGPointMake(self.bounds.size.width, 0);

	[self reset];

}

- (void)setFont:(UIFont *)font
{
	[_label1 setFont:font];
	[_label2 setFont:font];
}

- (UIFont *)font
{
	return _label1.font;
}

- (void)setTextColor:(UIColor *)textColor
{
	[_label1 setTextColor:textColor];
	[_label2 setTextColor:textColor];
}
- (UIColor *)textColor
{
	return _label1.textColor;
}
- (void)handleTapEvent
{
	if (_delegate &&
		[_delegate respondsToSelector:@selector(autoScrollLabel:tapIndex:)])
	{
		[_delegate autoScrollLabel:self tapIndex:_currentIndex];
	}

}

- (void)setTitleArray:(NSArray *)titleArray
{
	if (_titileArrayx)
	{
    }
	if (titleArray)
	{
		_titileArrayx = titleArray;
	}
	else
	{
		_titileArrayx = nil;
	}

	[self reset];
}


- (void)reset
{
	if (_titileArrayx &&
		[_titileArrayx count] > 0)
	{

		CGRect frame = _currentLabel.frame;
		frame.origin = _orginPoint;
		_label2.frame = frame;

		_currentLabel = _label1;

		frame.origin = CGPointMake(0, 0);
		_label1.frame = frame;

		_currentIndex = 0;
		_currentLabel.text = [_titileArrayx objectAtIndex:0];
	}

}

- (void)start
{
	if (!_started)
	{
		if (_titileArrayx &&
			[_titileArrayx count] > 0)
		{
			_currentLabel.text = [_titileArrayx objectAtIndex:0];
		}
		_timer = [NSTimer scheduledTimerWithTimeInterval:_interval
												  target:self
												selector:@selector(changeTheLabel:)
												userInfo:nil
												 repeats:YES];
		_started = YES;
	}
	else
	{
		NSLog(@"has started!");
	}

}

- (void)stop
{
	_started = NO;
}

- (void)changeTheLabel:(NSTimer *)timer
{
	if (_started)
	{
		_currentIndex ++;
		if (_currentIndex == [_titileArrayx count])
		{
			_currentIndex = 0;
		}
		self.text = [_titileArrayx objectAtIndex:_currentIndex];
	}
	else
	{
		[timer invalidate];
		timer = nil;
	}

}

- (void)setText:(NSString *)text
{
	UILabel *temLabel = _currentLabel;

	if (_currentLabel)
	{
		_currentLabel = (_currentLabel == _label1)?_label2:_label1;
	}
	temLabel = (_currentLabel == _label1)?_label2:_label1;

	_currentLabel.text = text;

	CGRect frame = _currentLabel.frame;
	frame.origin = _orginPoint;
	_currentLabel.frame = frame;

	[UIView beginAnimations:@"" context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:0.5];
	
	frame.origin = CGPointMake(0, 0);
	_currentLabel.frame = frame;

	frame.origin = CGPointMake(- self.bounds.size.width, 0);
	temLabel.frame = frame;
	[UIView commitAnimations];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
